<template>
  <div class="admin-box margin-right-sm">
    <div class="admin-content">
      <div class="contents">
        <div class="querys">
          <el-form :model="queryParams" ref="queryForm" :inline="true">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入" clearable size="small"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="所属组织" prop="name">
              <el-input v-model="queryParams.enterpriseName" placeholder="请输入" clearable size="small"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="用户编码" prop="auditStatus">
              <el-input v-model="queryParams.account" placeholder="请输入" clearable size="small"
                        style="width: 240px"
                        @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item>
              <el-button  type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询
              </el-button>
              <el-button  icon="el-icon-refresh" class="border-Blue" size="mini" @click="resetQuery">
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="opt_header">
          <el-button @click="handleAdd" type="primary">新增用户</el-button>
          <el-button v-preventReClick  plain="" size="mini" @click="handleExport" :loading="exportLoading"
                     v-hasPermi="['system:role:export']">批量导入用户</el-button>
        </div>
        <el-table
            :data="tableData"
            style="width: 100%">
          <el-table-column prop="name" label="姓名" :show-overflow-tooltip="true"/>
          <el-table-column prop="account" label="用户编码"/>
          <el-table-column prop="phone" label="用户手机号">
            <template slot-scope="scope">
              {{maskPhoneNumber(scope.row.phone)}}
            </template>
          </el-table-column>
          <el-table-column prop="enterpriseName" label="所属组织"/>
<!--          <el-table-column prop="realNameStatus" label="实名状态">-->
<!--            <template slot-scope="scope">-->
<!--              <el-tag :type="scope.row.realNameStatus =='1' ? 'success' : ''">-->
<!--                {{ calReal(scope.row) }}-->
<!--              </el-tag>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column prop="createTime" label="创建时间">
            <template slot-scope="scope">
              <span> {{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="160">
            <template slot-scope="scope">
              <div class="opt_table">
                <el-button type="text" @click="handleShow(scope.row)">查看</el-button>
                <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
                <el-popconfirm
                    icon-color="red"
                    title="确认删除？"
                    @confirm="confirm(scope.row)"
                >
                  <el-button style="position: relative;left:10px;" type="text" slot="reference">删除</el-button>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="opt_page" v-show="queryParams.total>0">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="queryParams.pageNo"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="queryParams.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryParams.total">
          </el-pagination>
        </div>

        <addUser ref="addUser" @getList="getList" />
        <editUser ref="editUser" @getList="getList" />
        <showUser ref="showUser" />
      </div>
    </div>
  </div>
</template>
<script>
import {parseTime} from '@/utils/ruoyi'
import addUser from "@/views/organization/user/list/addUser.vue";
import editUser from "@/views/organization/user/list/editUser.vue";
import showUser from "@/views/organization/user/list/showUser.vue";
import {findRelativeUserList,delRelativeUser} from "@/api/organizationOuter/userApi";
import {maskEmail, maskPhoneNumber} from "@/utils/fengwenxin_utils";
import bus, {busEvent} from "@/utils/bus";

export default {
  components: {
    addUser,
    showUser,
    editUser,
  },
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
        name: '', // 企业名称
        account: '',
        enterpriseName:'', // 所属组织
      },
      tableData: [],
    }
  },
  created() {
    this.getList()
    bus.$on(busEvent.organization_user,()=>{
      this.getList()
    })
  },
  methods: {
    maskPhoneNumber(val){
      return maskPhoneNumber(val)
    },
    maskEmail(val){
      return maskEmail(val)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        name: '',
        account: '',
        enterpriseName:''
      }
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryParams.pageNo = 1;
      this.queryParams.pageSize = val;
      this.getList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryParams.pageNo = val;
      this.getList()
    },
    initEtpList() {
      const {pageSize, pageNo, name, account,enterpriseName} = this.queryParams;
      findRelativeUserList({
        pageSize,
        pageNo,
        name,
        account,
        enterpriseName
      }).then(res => {
        console.log('res', res)
        const list = res.data.list;
        this.tableData = list;
        this.queryParams.total = res.data.total;
      }).catch(err => {
        console.log('err', err)

      })
    },
    parseTime(val) {
      return parseTime(val)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.addUser.handleadd('0')
    },
    /** 批量导入用户按钮操作 */
    handleExport() {
      //const queryParams = this.queryParams;
      this.$store.dispatch('uploadsOuter/SETOPEN',true)
      this.$store.dispatch('uploadsOuter/SETTYPE',1)
      this.$store.dispatch('uploadsOuter/SETACTIVE',0)
      this.$store.dispatch('uploadsOuter/SETTITLE','批量导入用户')
    },
    handleTab(tab, event) {
      // console.log(tab.index);
      this.queryParams.pageNo = 1;
      this.queryParams.pageSize = 10;
      this.getList()
    },
    getList() {
        this.initEtpList()
    },
    handleShow(row) {
      this.$refs.showUser.show(row)
    },
    handleEdit(row) {
      this.$refs.editUser.show(row)
    },
    confirm(row) {
      // console.log('row',row)
      const id = row.id;
      delRelativeUser({id}).then((response) => {
        this.$message({
          message: '删除成功',
          type: 'success'
        });
        this.getList()
      }).catch(err => {
        console.error('err', err)
      });
    },
    calReal(row) {
      const isReal = row.realNameStatus == '1';
      return isReal ? '已实名' : '未实名';
    },
  }
}
</script>
<style scoped lang="less">
.admin-content {
  margin: 4px 0 0 4px;
  padding: 20px;
  background: #fff;
  //border:1px solid red;
  overflow: auto;
  max-height: 95%;
  min-height: 95%;

  .contents {

    .opt_header {
      padding-bottom: 10px;
    }

    .opt_page {
      padding-top: 20px;
    }
  }
}
</style>
